﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BYManageInfoSystem
{
    public partial class Frm_RecvFacFee : Form
    {
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.DZCompDataTab m_bllDZCompDataTab = new DGBY.BLL.DZCompDataTab();
        private DGBY.BLL.HKShipCompDataTab m_bllHKShipCompDataTab = new DGBY.BLL.HKShipCompDataTab();
        private DGBY.BLL.HKTransCompDataTab m_bllHKTransCompDataTab = new DGBY.BLL.HKTransCompDataTab();
        private DGBY.BLL.DHLDataTab m_bllDHLDataTab = new DGBY.BLL.DHLDataTab();
        private DGBY.BLL.TCCompDataTab m_bllTCCompDataTab = new DGBY.BLL.TCCompDataTab();

        private DGBY.BLL.FeeTab m_bllFeeTab = new DGBY.BLL.FeeTab();
        private DGBY.BLL.FacFeeRelationTab m_bllFacFeeRelationTab = new DGBY.BLL.FacFeeRelationTab();
        private DGBY.BLL.FacFeeTab m_bllFacFeeTab = new DGBY.BLL.FacFeeTab();
        private DGBY.BLL.BYFeeKaiPiaoCateTab m_bllBYFeeKaiPiaoCateTab = new DGBY.BLL.BYFeeKaiPiaoCateTab();
        private DGBY.BLL.FeeInvoiceTab m_bllFeeInvoiceTab = new DGBY.BLL.FeeInvoiceTab();
        private DGBY.BLL.FeeFacInvoiceRelationTab m_bllFeeFacInvoiceRelationTab = new DGBY.BLL.FeeFacInvoiceRelationTab();
        
        private int m_nSelectFacFeeTabID = 0;
        public Frm_RecvFacFee()
        {
            InitializeComponent();
        }
        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorWidth = 40;
        }

        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }

        private void InitCombox()
        {
            DataSet ds = m_bllPaidanRegisterTab.GetNameAndID("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = ds.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxContractNo.SelectedIndex = 0;
            }

            ds = m_bllTCCompDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxTransComp.DataSource = ds.Tables[0];
                this.comboBoxTransComp.ValueMember = "TCCompDataTabID";
                this.comboBoxTransComp.DisplayMember = "拖车公司简称";
                this.comboBoxTransComp.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxTransComp.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxTransComp.SelectedIndex = 0;
            }

            ds = m_bllHKShipCompDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxHKShipName.DataSource = ds.Tables[0];
                this.comboBoxHKShipName.ValueMember = "HKShipCompDataTabID";
                this.comboBoxHKShipName.DisplayMember = "香港船务公司名称";
                this.comboBoxHKShipName.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxHKShipName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxHKShipName.SelectedIndex = 0;
            }

            ds = m_bllDHLDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDHLName.DataSource = ds.Tables[0];
                this.comboBoxDHLName.ValueMember = "DHLDataTabID";
                this.comboBoxDHLName.DisplayMember = "快递公司简称";
                this.comboBoxDHLName.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxDHLName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxDHLName.SelectedIndex = 0;
            }

            ds = m_bllHKTransCompDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxHKTransName.DataSource = ds.Tables[0];
                this.comboBoxHKTransName.ValueMember = "HKTransCompDataTabID";
                this.comboBoxHKTransName.DisplayMember = "香港运输公司名称";
                this.comboBoxHKTransName.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxHKTransName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxHKTransName.SelectedIndex = 0;
            }
            ds = m_bllDZCompDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDZComp.DataSource = ds.Tables[0];
                this.comboBoxDZComp.ValueMember = "DZCompDataTabID";
                this.comboBoxDZComp.DisplayMember = "公司简称";
                this.comboBoxDZComp.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxDZComp.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxDZComp.SelectedIndex = 0;
            }
        }
       
        private void Frm_RecvFacFee_Load(object sender, EventArgs e)
        {
            LocalPrintPreveiw();
            InitUltraWinGridSetting();
            InitCombox();
        }
        //清空textbox
        private void ResetAllTextBox(Form form)
        {
            for (int i = 0; i < form.Controls.Count; i++)
            {
                if (form.Controls[i] is TextBox)
                {
                    (form.Controls[i] as TextBox).Text = "";
                }

                if (form.Controls[i] is GroupBox)
                {
                    for (int j = 0; j < form.Controls[i].Controls.Count; j++)
                    {
                        if (form.Controls[i].Controls[j] is TextBox)
                        {
                            (form.Controls[i].Controls[j] as TextBox).Text = "";
                        }
                    }
                }
            }
        }
        private void ShowContractSheetByContractNo(string strContractNo)
        {
            int nID = Convert.ToInt32(strContractNo, 10);

            DataSet ds = m_bllFacFeeTab.ShowRecvFacFeeRec("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");
            ultraGrid1.DataSource = null;

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                //ResetAllTextBox(this);
                this.textBoxBYComp.Text = ds.Tables[0].Rows[0]["经营单位"].ToString();
                this.textBoxDingDanNo.Text = ds.Tables[0].Rows[0]["订单号"].ToString();
                this.textBoxFacName.Text = ds.Tables[0].Rows[0]["工厂名称"].ToString();
                this.textBoxBGHName.Text = ds.Tables[0].Rows[0]["报关行名称"].ToString();
                this.textBoxBGDNo.Text = ds.Tables[0].Rows[0]["报关单号"].ToString();
                this.textBoxHXDNo.Text = ds.Tables[0].Rows[0]["核销单号"].ToString();
                this.textBoxBiZhi.Text = ds.Tables[0].Rows[0]["币种"].ToString();
                this.textBoxZGPort.Text = ds.Tables[0].Rows[0]["装运港"].ToString();
                this.textBoxBGJinE.Text = ds.Tables[0].Rows[0]["报关金额"].ToString();
                this.dateTimePickerSBDate.Value = (DateTime)ds.Tables[0].Rows[0]["申报日期"];

            }
            else
            {
                ds = m_bllFacFeeTab.ShowOneRecvFacFeeRec("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");


                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.textBoxBYComp.Text = ds.Tables[0].Rows[0]["经营单位"].ToString();
                    this.textBoxDingDanNo.Text = ds.Tables[0].Rows[0]["订单号"].ToString();
                    this.textBoxFacName.Text = ds.Tables[0].Rows[0]["工厂名称"].ToString();
                    this.textBoxBGHName.Text = ds.Tables[0].Rows[0]["报关行名称"].ToString();
                    this.textBoxBGDNo.Text = ds.Tables[0].Rows[0]["报关单号"].ToString();
                    this.textBoxHXDNo.Text = ds.Tables[0].Rows[0]["核销单号"].ToString();
                    this.textBoxBiZhi.Text = ds.Tables[0].Rows[0]["币种"].ToString();
                    this.textBoxZGPort.Text = ds.Tables[0].Rows[0]["装运港"].ToString();
                    this.textBoxBGJinE.Text = ds.Tables[0].Rows[0]["报关金额"].ToString();
                    this.dateTimePickerSBDate.Value = (DateTime)ds.Tables[0].Rows[0]["申报日期"];

                }
            }


        }
        private void comboBoxContractNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxContractNo.SelectedValue.ToString();
                ShowContractSheetByContractNo(str);

            }
            catch { }
        }

        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllFacFeeTab.ShowRecvFacFeeRec(strWhere);
            ultraGrid1.DataSource = null;

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ResetAllTextBox(this);

            }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }
        /// 取得某月的第一天
        /// </summary>
        /// <param name="datetime">要取得月份第一天的时间</param>
        /// <returns></returns>
        private DateTime FirstDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day);
        }
        /// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        private DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShipPubHuiKuan))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShipPrivateHuiKuan))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxSJJinE))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShouldPaySJ))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShouldRecvSJ))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxTransFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxBGFeeShouldPay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShouldPayDZFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxOtherFeeShouldPay))
                    return;
                if (ClassInputValid.IsNum(this.errorProvider1, this.textBoxTCGuiCounts))
                    return;

                DGBY.Model.FacFeeTab modelFacFeeTab = new DGBY.Model.FacFeeTab();
                modelFacFeeTab.ShipDLPayMoney = Convert.ToDecimal(this.textBoxShipPubHuiKuan.Text);
                modelFacFeeTab.ShipDLRecvMoney = Convert.ToDecimal(this.textBoxShipPrivateHuiKuan.Text);
                modelFacFeeTab.SJJinE = Convert.ToDecimal(this.textBoxSJJinE.Text);
                modelFacFeeTab.ShouldPaySJJinE = Convert.ToDecimal(this.textBoxShouldPaySJ.Text);
                modelFacFeeTab.ShouldRecvSJJinE = Convert.ToDecimal(this.textBoxShouldRecvSJ.Text);
                modelFacFeeTab.ShouldPayTransFee = Convert.ToDecimal(this.textBoxTransFee.Text);
                modelFacFeeTab.ShouldPayBGFee = Convert.ToDecimal(this.textBoxBGFeeShouldPay.Text);

                modelFacFeeTab.ShouldPayDZFee = Convert.ToDecimal(this.textBoxShouldPayDZFee.Text);
                modelFacFeeTab.ShouldPayDHLFee = Convert.ToDecimal(this.textBoxOtherFeeShouldPay.Text);

                modelFacFeeTab.TCGuiCounts = Convert.ToInt32(this.textBoxTCGuiCounts.Text, 10);

                if (this.comboBoxContractNo.SelectedValue != null)
                    modelFacFeeTab.PaidanRegisterTabID = Convert.ToInt32(this.comboBoxContractNo.SelectedValue.ToString(), 10);

                if (this.comboBoxTransComp.SelectedValue != null)
                    modelFacFeeTab.TCCompDataTabID = Convert.ToInt32(this.comboBoxTransComp.SelectedValue.ToString(), 10);
                if (this.comboBoxDHLName.SelectedValue != null)
                    modelFacFeeTab.DHLDataTabID = Convert.ToInt32(this.comboBoxDHLName.SelectedValue.ToString(), 10);

                if (this.comboBoxHKShipName.SelectedValue != null)
                    modelFacFeeTab.HKShipCompDataTabID = Convert.ToInt32(this.comboBoxHKShipName.SelectedValue.ToString(), 10);
                if (this.comboBoxHKTransName.SelectedValue != null)
                    modelFacFeeTab.HKTransCompDataTabID = Convert.ToInt32(this.comboBoxHKTransName.SelectedValue.ToString(), 10);

                if (this.comboBoxDZComp.SelectedValue != null)
                    modelFacFeeTab.DZCompDataTabID = Convert.ToInt32(this.comboBoxDZComp.SelectedValue.ToString(), 10);

                int nRetID = m_bllFacFeeTab.Add(modelFacFeeTab);

                DataSet ds = m_bllFeeTab.GetList("");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DGBY.Model.FacFeeRelationTab modelFacFeeRelationTab = new DGBY.Model.FacFeeRelationTab();
                    modelFacFeeRelationTab.FacFeeTabID = nRetID;

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        int nFeeTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["FeeTabID"].ToString(), 10);

                        modelFacFeeRelationTab.FeeTabID = nFeeTabID;
                        m_bllFacFeeRelationTab.Add(modelFacFeeRelationTab);
                    }
                }
                ShowAllRecord("");

                ds = m_bllFacFeeTab.ShowRecvFacFeeRec("FacFeeTab.FacFeeTabID = '" + nRetID.ToString() + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DateTime dtSB = (DateTime)ds.Tables[0].Rows[0]["申报日期"];
                    DateTime dtFirstDay = FirstDayOfMonth(dtSB);
                    DateTime dtLastDay = LastDayOfMonth(dtSB);
                    string strFacName = ds.Tables[0].Rows[0]["工厂名称"].ToString();
                    ds = m_bllFacFeeTab.ShowRecvFacFeeRec("FactoryDataTab.FactoryName = '" + strFacName.ToString() + "'"
                                                                + " and BGDComeBackTab.SBDate >= '" + dtFirstDay + "'" + " and BGDComeBackTab.SBDate <= '" + dtLastDay + "'");

                    if (ds.Tables.Count > 0 /*&& ds.Tables[0].Rows.Count <= 1*/)
                    {
                        DataSet ds2 = m_bllBYFeeKaiPiaoCateTab.GetList("");
                        if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                        {
                            for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                            {
                                int nId = Convert.ToInt32(ds2.Tables[0].Rows[i]["BYFeeKaiPiaoCateTabID"].ToString(), 10);


                                DGBY.Model.FeeInvoiceTab modelFeeInvoiceTab = new DGBY.Model.FeeInvoiceTab();
                                string strTemp = ds2.Tables[0].Rows[i]["开票明细分类名"].ToString();
                                int nIndex = strTemp.IndexOf("百业收据");
                                if (nIndex >= 0)
                                    modelFeeInvoiceTab.IsPrivate = 1;
                                modelFeeInvoiceTab.BYFeeKaiPiaoCateTabID = nId;
                                modelFeeInvoiceTab.InvoiceDate = DateTime.Now.Date;
                                modelFeeInvoiceTab.PostDate = DateTime.Now.Date;
                                int nReturnId = m_bllFeeInvoiceTab.Add(modelFeeInvoiceTab);
                                DGBY.Model.FeeFacInvoiceRelationTab modelFeeFacInvoiceRelationTab = new DGBY.Model.FeeFacInvoiceRelationTab();
                                modelFeeFacInvoiceRelationTab.FacFeeTabID = nRetID;
                                modelFeeFacInvoiceRelationTab.FeeInvoiceTabID = nReturnId;
                                m_bllFeeFacInvoiceRelationTab.Add(modelFeeFacInvoiceRelationTab);
                            }
                        }
                    }
                }
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.FacFeeTab modelFacFeeTab = m_bllFacFeeTab.GetModel(m_nSelectFacFeeTabID);
                if (modelFacFeeTab == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShipPubHuiKuan))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShipPrivateHuiKuan))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxSJJinE))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShouldPaySJ))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShouldRecvSJ))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxTransFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxBGFeeShouldPay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShouldPayDZFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxOtherFeeShouldPay))
                    return;
                if (ClassInputValid.IsNum(this.errorProvider1, this.textBoxTCGuiCounts))
                    return;

                modelFacFeeTab.ShipDLPayMoney = Convert.ToDecimal(this.textBoxShipPubHuiKuan.Text);
                modelFacFeeTab.ShipDLRecvMoney = Convert.ToDecimal(this.textBoxShipPrivateHuiKuan.Text);
                modelFacFeeTab.SJJinE = Convert.ToDecimal(this.textBoxSJJinE.Text);
                modelFacFeeTab.ShouldPaySJJinE = Convert.ToDecimal(this.textBoxShouldPaySJ.Text);
                modelFacFeeTab.ShouldRecvSJJinE = Convert.ToDecimal(this.textBoxShouldRecvSJ.Text);
                modelFacFeeTab.ShouldPayTransFee = Convert.ToDecimal(this.textBoxTransFee.Text);
                modelFacFeeTab.ShouldPayBGFee = Convert.ToDecimal(this.textBoxBGFeeShouldPay.Text);

                modelFacFeeTab.ShouldPayDZFee = Convert.ToDecimal(this.textBoxShouldPayDZFee.Text);
                modelFacFeeTab.ShouldPayDHLFee = Convert.ToDecimal(this.textBoxOtherFeeShouldPay.Text);

                modelFacFeeTab.TCGuiCounts = Convert.ToInt32(this.textBoxTCGuiCounts.Text, 10);

                if (this.comboBoxContractNo.SelectedValue != null)
                    modelFacFeeTab.PaidanRegisterTabID = Convert.ToInt32(this.comboBoxContractNo.SelectedValue.ToString(), 10);

                if (this.comboBoxTransComp.SelectedValue != null)
                    modelFacFeeTab.TCCompDataTabID = Convert.ToInt32(this.comboBoxTransComp.SelectedValue.ToString(), 10);
                if (this.comboBoxDHLName.SelectedValue != null)
                    modelFacFeeTab.DHLDataTabID = Convert.ToInt32(this.comboBoxDHLName.SelectedValue.ToString(), 10);

                if (this.comboBoxHKShipName.SelectedValue != null)
                    modelFacFeeTab.HKShipCompDataTabID = Convert.ToInt32(this.comboBoxHKShipName.SelectedValue.ToString(), 10);
                if (this.comboBoxHKTransName.SelectedValue != null)
                    modelFacFeeTab.HKTransCompDataTabID = Convert.ToInt32(this.comboBoxHKTransName.SelectedValue.ToString(), 10);

                if (this.comboBoxDZComp.SelectedValue != null)
                    modelFacFeeTab.DZCompDataTabID = Convert.ToInt32(this.comboBoxDZComp.SelectedValue.ToString(), 10);


                m_bllFacFeeTab.Update(modelFacFeeTab);
                ShowAllRecord("");
                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (m_bllFacFeeTab.GetModel(m_nSelectFacFeeTabID) == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllFacFeeTab.Delete(m_nSelectFacFeeTabID);

                ShowAllRecord("");
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ResetAllTextBox(this);
                return;
            }
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            string strFacFeeTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectFacFeeTabID = Convert.ToInt32(strFacFeeTabID, 10);
            cellFacFeeTabClick();
        }

        private void cellFacFeeTabClick()
        {
            
            DGBY.Model.FacFeeTab FacFeeTabModel = m_bllFacFeeTab.GetModel(m_nSelectFacFeeTabID);
            if (FacFeeTabModel != null)
            {
                DataSet ds = m_bllFacFeeRelationTab.GetTotalFee("FacFeeRelationTab.FacFeeTabID = '" + m_nSelectFacFeeTabID.ToString() + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.textBoxFeeTotal.Text = ds.Tables[0].Rows[0]["总费用"].ToString();
                }
                this.textBoxShipPubHuiKuan.Text = FacFeeTabModel.ShipDLPayMoney.ToString();
                this.textBoxShipPrivateHuiKuan.Text = FacFeeTabModel.ShipDLRecvMoney.ToString();
                this.textBoxSJJinE.Text = FacFeeTabModel.SJJinE.ToString();
                this.textBoxShouldPaySJ.Text = FacFeeTabModel.ShouldPaySJJinE.ToString();
                this.textBoxShouldRecvSJ.Text = FacFeeTabModel.ShouldRecvSJJinE.ToString();
                this.textBoxTransFee.Text = FacFeeTabModel.ShouldPayTransFee.ToString();
                this.textBoxBGFeeShouldPay.Text = FacFeeTabModel.ShouldPayBGFee.ToString();
                this.textBoxShouldPayDZFee.Text = FacFeeTabModel.ShouldPayDZFee.ToString();
                this.textBoxOtherFeeShouldPay.Text = FacFeeTabModel.ShouldPayDHLFee.ToString();
                this.textBoxTCGuiCounts.Text = FacFeeTabModel.TCGuiCounts.ToString();

                if (FacFeeTabModel.PaidanRegisterTabID != null)
                    this.comboBoxContractNo.SelectedValue = FacFeeTabModel.PaidanRegisterTabID;
                if (FacFeeTabModel.TCCompDataTabID != null)
                    this.comboBoxTransComp.SelectedValue = FacFeeTabModel.TCCompDataTabID;
                if (FacFeeTabModel.DHLDataTabID != null)
                    this.comboBoxDHLName.SelectedValue = FacFeeTabModel.DHLDataTabID;
                if (FacFeeTabModel.HKShipCompDataTabID != null)
                    this.comboBoxHKShipName.SelectedValue = FacFeeTabModel.HKShipCompDataTabID;
                if (FacFeeTabModel.HKTransCompDataTabID != null)
                    this.comboBoxHKTransName.SelectedValue = FacFeeTabModel.HKTransCompDataTabID;
                if (FacFeeTabModel.DZCompDataTabID != null)
                    this.comboBoxDZComp.SelectedValue = FacFeeTabModel.DZCompDataTabID;
            }
        }

        private void buttonGuiInfo_Click(object sender, EventArgs e)
        {
            DGBY.Model.FacFeeTab FacFeeTabModel = m_bllFacFeeTab.GetModel(m_nSelectFacFeeTabID);
            if (FacFeeTabModel != null)
            {
                DataSet ds = m_bllFacFeeTab.ShowOneRecvFacFeeRec("PaidanRegisterTab.PaidanRegisterTabID = '" + FacFeeTabModel.PaidanRegisterTabID + "'");


                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    int nBGDComeBackTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BGDComeBackTabID"].ToString(), 10);
                    Frm_FeeGuiInfo frm_FeeGuiInfo = new Frm_FeeGuiInfo(nBGDComeBackTabID);
                    frm_FeeGuiInfo.StartPosition = FormStartPosition.CenterScreen;
                    frm_FeeGuiInfo.ShowDialog();

                }
            }

        }

        private void buttonShipOther_Click(object sender, EventArgs e)
        {
            Frm_FacFeeRelation frm_FacFeeRelation = new Frm_FacFeeRelation(m_nSelectFacFeeTabID, "船务");
            frm_FacFeeRelation.StartPosition = FormStartPosition.CenterScreen;
            frm_FacFeeRelation.ShowDialog();
        }

        private void buttonTCOtherFee_Click(object sender, EventArgs e)
        {
            Frm_FacFeeRelation frm_FacFeeRelation = new Frm_FacFeeRelation(m_nSelectFacFeeTabID, "运输");
            frm_FacFeeRelation.StartPosition = FormStartPosition.CenterScreen;
            frm_FacFeeRelation.ShowDialog();
        }

        private void buttonDZOtherFee_Click(object sender, EventArgs e)
        {
            Frm_FacFeeRelation frm_FacFeeRelation = new Frm_FacFeeRelation(m_nSelectFacFeeTabID, "单证");
            frm_FacFeeRelation.StartPosition = FormStartPosition.CenterScreen;
            frm_FacFeeRelation.ShowDialog();
        }

        private void buttonSJOther_Click(object sender, EventArgs e)
        {
            Frm_FacFeeRelation frm_FacFeeRelation = new Frm_FacFeeRelation(m_nSelectFacFeeTabID, "商检");
            frm_FacFeeRelation.StartPosition = FormStartPosition.CenterScreen;
            frm_FacFeeRelation.ShowDialog();
        }

        private void buttonBGOther_Click(object sender, EventArgs e)
        {
            Frm_FacFeeRelation frm_FacFeeRelation = new Frm_FacFeeRelation(m_nSelectFacFeeTabID, "报关");
            frm_FacFeeRelation.StartPosition = FormStartPosition.CenterScreen;
            frm_FacFeeRelation.ShowDialog();
        }

        private void buttonOtherOther_Click(object sender, EventArgs e)
        {
            Frm_FacFeeRelation frm_FacFeeRelation = new Frm_FacFeeRelation(m_nSelectFacFeeTabID, "其他");
            frm_FacFeeRelation.StartPosition = FormStartPosition.CenterScreen;
            frm_FacFeeRelation.ShowDialog();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }

    }
}
